package com.winchain.chainbill.receivables.biz.mod.service.impl;

import com.shell.bee.base.utils.StringUtil;
import com.winchain.chainbill.ChainBillUtil;
import com.winchain.chainbill.basic.context.TimeoutOperation;
import com.winchain.chainbill.receivables.base.po.ArOperation;
import com.winchain.chainbill.receivables.base.po.ArRecord;
import com.winchain.chainbill.receivables.base.service.ArRecordService;
import com.winchain.chainbill.receivables.biz.impt.rx.BatchReceivablesRevEvent;
import com.winchain.chainbill.receivables.biz.impt.rx.BatchReceivablesStats;
import com.winchain.chainbill.receivables.biz.mod.rx.ReceivablesModRevSubscriber;
import com.winchain.chainbill.receivables.biz.mod.service.ReceivablesModRevService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import rx.Observable;

@Service("receivablesModRevService")
public class ReceivablesModRevServiceImpl implements ReceivablesModRevService {
	@Autowired
	private ArRecordService arRecordService;
	@Autowired
	private ReceivablesModRevSubscriber receivablesModRevSubscriber;

	public BatchReceivablesStats pass(String... arIds) {
		List<ArRecord> records = this.arRecordService.selectRecordsByIds(arIds);

		BatchReceivablesRevEvent event = new BatchReceivablesRevEvent();
		event.setRecs(records);

		ArOperation opt = new ArOperation();
		opt.setBizType("ar_mod");
		opt.setState("3");
		opt.setOptCode("07");
		opt.setNextState("4");
		String limitDate = ChainBillUtil.getLimitDate("AR_MOD_APRV_LIM_DATE");
		opt.setLimitDate(limitDate);
		opt.setRemark("������������");

		TimeoutOperation timeoutOpt = new TimeoutOperation();
		timeoutOpt.setOptCode("B2");

		timeoutOpt.setRemark("��������,������������");

		BatchReceivablesStats stats = new BatchReceivablesStats();

		event.setOperation(opt);
		event.setTimeoutOperation(timeoutOpt);
		event.setStats(stats);

		Observable<BatchReceivablesRevEvent> observers = Observable.just(event);

		observers.subscribe(this.receivablesModRevSubscriber);

		return stats;
	}

	public BatchReceivablesStats reject(String remark, String... arIds) {
		List<ArRecord> records = this.arRecordService.selectRecordsByIds(arIds);

		BatchReceivablesRevEvent event = new BatchReceivablesRevEvent();
		event.setRecs(records);

		ArOperation opt = new ArOperation();
		opt.setBizType("ar_mod");
		opt.setState("3");
		opt.setOptCode("08");
		opt.setNoTimeout(Boolean.valueOf(true));
		if (StringUtil.empty(remark)) {
			remark = "����������������";
		}
		opt.setRemark(remark);

		BatchReceivablesStats stats = new BatchReceivablesStats();

		event.setOperation(opt);
		event.setStats(stats);

		Observable<BatchReceivablesRevEvent> observers = Observable.just(event);

		observers.subscribe(this.receivablesModRevSubscriber);

		return stats;
	}
}
